package com.banmao.algorithm.No301To400.No438;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *  774ms，超慢
 */
public class Solution1 {

    public List<Integer> findAnagrams(String s, String p) {
        // 将p进行编码
        String pEncode = encode(p.toCharArray());

        ArrayList<Integer> res = new ArrayList<Integer>();

        // 遍历s
        int left = 0;
        while(left <= s.length() - p.length()) {
            String encode = encode(s.substring(left, left + p.length()).toCharArray());
            if(encode.equals(pEncode)) {
                res.add(left);
            }
            left++;
        }
        return res;

    }

    // 编码一个数组
    private String encode(char[] input) {
        int[] counts = new int[26];
        for (char c : input) {
            counts[c - 'a']++;
        }
        return Arrays.toString(counts);
    }

}
